<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/ ' }">
        <i class="el-icon-s-promotion"/>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
      <el-breadcrumb-item>第一步：选择类别</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="商品类别" prop="categoryId">
          <el-input v-model="ruleForm.categoryId"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        categoryId:'24',
        categoryName:''
      },
      rules: {
        categoryId: [
          { required: true, message: '请输入商品类别', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:9080/categories/"+this.ruleForm.categoryId+"/select"
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .get(url).then((response) =>{
            let responseBody = response.data;
            if(responseBody.state==20000){
              let category = responseBody.data;
              if (category.isParent == 1) {
                this.$message.error("错误，选择的类别存在子级类别")
              } else {
                this.ruleForm.categoryName = category.name;
                localStorage.setItem('categoryItem',JSON.stringify(this.ruleForm))
                this.$router.push('add-new-02');
              }
            }else {
              this.$message.error(responseBody.message)
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
